<template>
  <div>
    <a-card :bordered='false'>
      <f-search-form :more="true" @searchHandler="searchHandler">
        <f-search-form-item label="开始日期" type="date" name="beginDate" />
        <f-search-form-item label="结束日期" type="date" name="endDate"/>
      </f-search-form>
      <f-table ref='table' rowKey='drugsPlanId' :columns='columns' :multiple='true' :operator='operator' :params='queryParam' :api='api.drugsPlan' handler='drugsPlanPageListHandler'>
        <span slot='checkState' slot-scope='text, record, index'>
            <a-tag color='pink' v-if='text==0'>未提交</a-tag>
            <a-tag color='#87d068' v-if='text==1' @click='processImg(record)'>{{ record.taskName }}</a-tag>
            <a-tag color='#87d068' v-if='text==2 &(record.planAmount>record.advancePayment)'>预付款未提交</a-tag>
            <a-tag color='#87d068' v-if='text==2 &(record.planAmount==record.advancePayment)'>全额未提交</a-tag>
            <a-tag color='#f50' v-if='text==3' @click='processImg(record)'>{{ record.taskName }}</a-tag>
            <a-tag color='#87d068' v-if='text==4'>尾款审批未提交</a-tag>
            <a-tag color='#f50' v-if='text==5' @click='processImg(record)'>{{ record.taskName }}</a-tag>
            <a-tag color='#f50' v-if='text==6'>尾款审批结束</a-tag>
            <a-tag color='#f50' v-if='text==7' @click='processImg(record)'>{{ record.taskName }}</a-tag>
            <a-tag color='#f50' v-if='text==8'>全额审批结束</a-tag>
            <a-tag color='#f50' v-if='text==9'>已入库</a-tag>
        </span>
        <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='startProcess(record)' v-if='record.checkState==0'>提交审批</a>
            <a @click='startProcess(record)' v-if='record.checkState==2 &(record.planAmount>record.advancePayment)'>提交审批</a>
            <a @click='startProcess(record)' v-if='record.checkState==2 &(record.planAmount==record.advancePayment)'>提交审批</a>
            <a @click='startProcess(record)' v-if='record.checkState==4 '>尾款审批</a>
            <a @click='completeTask(record)' v-if="record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__'">提交审批</a>
            <a-divider type='vertical' v-if="record.checkState==0||record.checkState==2||record.checkState==4||(record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__')" />
            <a @click='$refs.drawer.showDrawer(record)' v-if='record.checkState==0'>编辑</a>
            <a-divider v-if='record.checkState==6||record.checkState==8' type='vertical' />
            <a v-if='record.checkState==6||record.checkState==8' @click='$refs.drugsLotDrawer.showDrawer(record)'>入库</a>
            <a-divider type='vertical' />
          </template>
          <a-dropdown  v-if='record.checkState==0'>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item v-if="$auth('table.delete')">
                <a href='javascript:;' @click='$refs.table.del(record)'>删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <bdp-drawer ref='drawer'></bdp-drawer>
      <drugs-lot-drawer ref='drugsLotDrawer'></drugs-lot-drawer>
    </a-card>
  </div>
</template>
<script>
import bdpDrawer from "./drugsplan_drawer.vue"
import drugsLotDrawer from "./drugslot_drawer.vue"
import ProcessImage from "../../flowable/process/process_image"
import api from "./js/api"

export default {
  name: "TableList",
  components: {
    bdpDrawer,
    drugsLotDrawer,
    ProcessImage
  },
  provide() {
    return {
      drugsplan: this
    };
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "添加",
      icon: "plus",
      that: this,
      handler: function(table, that) {
        that.$refs.drawer.showDrawer()
      }
    })
    operator.push({
      name: "删除",
      icon: "delete",
      batch: true,
      handler: function(table) {
        table.remove()
      }
    })
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: "序号",
          width: "60",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "药品",
          align: "center",
          dataIndex: "drugsName"
        },
        {
          title: "采购数量",
          align: "center",
          dataIndex: "drugsNum"
        },
        {
          title: "采购价",
          align: "center",
          dataIndex: "costPrice"
        },
        {
          title: "预付款",
          align: "center",
          dataIndex: "advancePayment"
        },
        {
          title: "采购金额",
          align: "center",
          dataIndex: "planAmount"
        },
        {
          title: "尾款",
          align: "center",
          dataIndex: "returnAmount"
        },
        {
          title: "采购日期",
          align: "center",
          dataIndex: "planDate"
        },
        {
          title: "审批状态",
          align: "center",
          dataIndex: "checkState",
          scopedSlots: { customRender: "checkState" }
        },
        {
          title: "创建时间",
          align: "center",
          dataIndex: "createTime"
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator
    }
  },
  created() {
  },
  methods: {
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    },
    //重新提交
    completeTask(drugsPlan) {
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.tasktodo.complete,
        requestBody: true,
        data: {
          taskId: drugsPlan.taskId
        },
        success: function(me, data) {
          me.$refs.table.refresh()
        }
      })
    },
    refresh(){
       this.$refs.drugsLotDrawer.visibleChange(false)
      this.$refs.table.refresh();

    },
    startProcess(drugsPlan) {
      let data = {}
      if (drugsPlan.checkState == 0) {
        data = {
          handlerName: "drugsPlanStartProcessHandler",
          businessKey: drugsPlan.drugsPlanId
        }
      } else {
        data = {
          handlerName: "drugsPlanPaymentPayStartProcessHandler",
          businessKey: drugsPlan.drugsPlanId
        }
      }
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.processInstance.start,
        requestBody: true,
        data: data,
        success: function(me, data) {
          me.$refs.table.refresh()
        }
      })
    }
  }
}
</script>
